﻿#region Copyright (C) Sector7 Software Studio. All rights reserved
/*******************************************************************************************
 * Creation
 *      Author:         Wolf Lai
 *      Date:           2013/5/23 星期四 8:48:53
 *      Version:        
 * Modification 
 *      Author: 
 *      Date: 
 *      Description: 
 *      Version:
 *******************************************************************************************/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Sector7.Medical.EntityType;
using Sector7.Framework.DB;
using Sector7.Framework.Core;
using Sector7.Medical.Consts;
using Sector7.Medical.DAO.Utility;

namespace Sector7.Medical.DAO.Standard
{
    /// <summary>
    /// 药品价格数据访问对象
    /// </summary>
    public class DrugPriceDAO : DAOBase<M7_DrugPrice>, IDrugPriceDAO
    {
        #region Public functions
        
        public List<M7_DrugPrice> ReadAll(params EntityConditionBase[] conditions)
        {
            string sql = string.Format(@"
                SELECT *
                    FROM {0}", C_M7_TableName.DRUG_PRICE);

            var sqlParams = new List<SqlParameter>();
            for (int i = 0; i < conditions.Length; i++)
            {
                if (i == 0)
                {
                    sql += " WHERE";
                }
                var condition = conditions[i];
                sql += string.Format(" {0} = {1} {2}", condition.ConditionName, condition.ConditionValue,
                    (i < conditions.Length - 1) ? "AND" : "");
                sqlParams.Add(DBUtil.CreateSqlParameter(condition.ConditionValue, condition.ConditionName));
            }

            using (DBHelper helper = new DBHelper())
            {
                SqlDataReader reader = helper.ExecuteQuery(sql, sqlParams.ToArray());

                List<M7_DrugPrice> priceList = new List<M7_DrugPrice>();
                if (reader.Read())
                {
                    var price = new M7_DrugPrice();
                    DBUtil.ReadData(reader, price);

                    priceList.Add(price);
                }
                return priceList;
            }
        }

        public M7_DrugPrice Read(long drugId, long specId, int unitId)
        {
            M7_DrugPrice price = null;
            string sql = @"
                SELECT *
                FROM M7_DrugPrice p
                    LEFT JOIN M7_DrugSpec s ON p.SpecId = s.SpecId
                WHERE
                    p.DrugId = @DrugId AND
                    p.SpecId = @SpecId AND
                    p.UnitId = @UnitId";

            using (DBHelper helper = new DBHelper())
            {
                SqlDataReader reader = helper.ExecuteQuery(sql,
                    DBUtil.CreateSqlParameter(drugId, C_ColumnName.DRUG_ID),
                    DBUtil.CreateSqlParameter(specId, C_ColumnName.SPEC_ID),
                    DBUtil.CreateSqlParameter(unitId, C_ColumnName.UNIT_ID));
                if (reader.Read())
                {
                    price = new M7_DrugPrice();
                    DBUtil.ReadData(reader, price);

                    price.Spec = new M7_DrugSpec();
                    DBUtil.ReadData(reader, price.Spec);
                }
            }

            return price;
        }

        #endregion

        #region Structure
        public DrugPriceDAO()
        {
            m_tableName = C_M7_TableName.DRUG_PRICE;
            m_primaryKeyName = C_ColumnName.PRICE_ID;
        }
        #endregion
    }
}
